# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
import json
from ..items import ImageItem as imagesitem

class ExampleSpider(scrapy.Spider):
    name = 'fengling'
    BASE_URL = 'https://image.so.com/zjl?ch=beauty&t1=595&src=banner_beauty&gid=&sn=%s&listtype=new&temp=1'
    start_index = 30
    start_urls = [BASE_URL % '30']
    MAX_DOWNLOAD_NUM = 100
    def parse(self, response):
        infos = json.loads(response.body.decode('utf-8'))
        item = imagesitem()
        item['image_urls'] = [ info['qhimg_url'] for info in infos['list'] ]
        yield item
        self.start_index += infos['count']
        if infos['count'] > 0 and self.start_index < self.MAX_DOWNLOAD_NUM:
            yield scrapy.Request(self.BASE_URL % self.start_index)